#include "keystring.h"

KeyString::KeyString()
{
    this->mKeyMap[Qt::Key_Escape]="Esc";
    this->mKeyMap[Qt::Key_Tab]="Tab";
    this->mKeyMap[Qt::Key_Backtab]="Backtab";
    this->mKeyMap[Qt::Key_Backspace]="Backspace";
    this->mKeyMap[Qt::Key_Return]="Return";
    this->mKeyMap[Qt::Key_Insert]="Insert";
    this->mKeyMap[Qt::Key_Delete]="Delete";
    this->mKeyMap[Qt::Key_Pause]="Pause";
    this->mKeyMap[Qt::Key_Home]="Home";
    this->mKeyMap[Qt::Key_End]="End";
    this->mKeyMap[Qt::Key_Left]="Left";
    this->mKeyMap[Qt::Key_Up]="Up";
    this->mKeyMap[Qt::Key_Right]="Right";
    this->mKeyMap[Qt::Key_Down]="Down";
    this->mKeyMap[Qt::Key_PageUp]="PageUp";
    this->mKeyMap[Qt::Key_PageDown]="PageDown";
    this->mKeyMap[Qt::Key_F1]="F1";
    this->mKeyMap[Qt::Key_F2]="F2";
    this->mKeyMap[Qt::Key_F3]="F3";
    this->mKeyMap[Qt::Key_F4]="F4";
    this->mKeyMap[Qt::Key_F5]="F5";
    this->mKeyMap[Qt::Key_F6]="F6";
    this->mKeyMap[Qt::Key_F7]="F7";
    this->mKeyMap[Qt::Key_F8]="F8";
    this->mKeyMap[Qt::Key_F9]="F9";
    this->mKeyMap[Qt::Key_F10]="F10";
    this->mKeyMap[Qt::Key_F11]="F11";
    this->mKeyMap[Qt::Key_F12]="F12";
    this->mKeyMap[Qt::Key_Space]="Space";
    this->mKeyMap[Qt::Key_0]="0";
    this->mKeyMap[Qt::Key_1]="1";
    this->mKeyMap[Qt::Key_2]="2";
    this->mKeyMap[Qt::Key_3]="3";
    this->mKeyMap[Qt::Key_4]="4";
    this->mKeyMap[Qt::Key_5]="5";
    this->mKeyMap[Qt::Key_6]="6";
    this->mKeyMap[Qt::Key_7]="7";
    this->mKeyMap[Qt::Key_8]="8";
    this->mKeyMap[Qt::Key_9]="9";
    this->mKeyMap[Qt::Key_A]="A";
    this->mKeyMap[Qt::Key_B]="B";
    this->mKeyMap[Qt::Key_C]="C";
    this->mKeyMap[Qt::Key_D]="D";
    this->mKeyMap[Qt::Key_E]="E";
    this->mKeyMap[Qt::Key_F]="F";
    this->mKeyMap[Qt::Key_G]="G";
    this->mKeyMap[Qt::Key_H]="F";
    this->mKeyMap[Qt::Key_I]="I";
    this->mKeyMap[Qt::Key_J]="J";
    this->mKeyMap[Qt::Key_K]="K";
    this->mKeyMap[Qt::Key_L]="L";
    this->mKeyMap[Qt::Key_M]="M";
    this->mKeyMap[Qt::Key_N]="N";
    this->mKeyMap[Qt::Key_O]="O";
    this->mKeyMap[Qt::Key_P]="P";
    this->mKeyMap[Qt::Key_Q]="Q";
    this->mKeyMap[Qt::Key_R]="R";
    this->mKeyMap[Qt::Key_S]="S";
    this->mKeyMap[Qt::Key_T]="T";
    this->mKeyMap[Qt::Key_U]="U";
    this->mKeyMap[Qt::Key_V]="V";
    this->mKeyMap[Qt::Key_W]="W";
    this->mKeyMap[Qt::Key_X]="X";
    this->mKeyMap[Qt::Key_Y]="Y";
    this->mKeyMap[Qt::Key_Z]="Z";
}

QString KeyString::key2String(Qt::Key key, Qt::KeyboardModifiers mod)
{
    QString showtext="";
    QString keystr="";
    QStringList mods;

    if(Qt::ControlModifier & mod){
        mods.append("CTRL");
    }
    if(Qt::ShiftModifier & mod){
        mods.append("SHIFT");
    }
    if(Qt::AltModifier & mod){
        mods.append("ALT");
    }

    keystr=this->mKeyMap[key];

    if(mods.length()>0){
        showtext=mods.join("+")+"+"+keystr;
    }else{
        showtext=keystr;
    }

    return showtext;
}

void KeyString::string2Key(QString& str, Qt::Key& key, Qt::KeyboardModifiers& mod)
{
    QString modstr="";
    QString keystr="";

    QStringList keyList=str.split('+');

    if(keyList.size()<=1){
        keystr=keyList[0];
    }else{
        keystr=keyList.last();
        auto pos=str.lastIndexOf('+');
        modstr=str.left(pos);
    }

    if(modstr==""){
        mod=Qt::NoModifier;
    }else{
        if(modstr.contains("CTRL")){
            mod|=Qt::ControlModifier;
        }
        if(modstr.contains("SHIFT")){
            mod|=Qt::ShiftModifier;
        }
        if(modstr.contains("ALT")){
            mod|=Qt::AltModifier;
        }
    }

    key=Qt::Key(this->mKeyMap.keys(keystr)[0]);
}
